package com.vids.task;

import cn.hutool.core.util.ObjectUtil;
import com.vids.constant.Urlconstant;
import com.vids.constant.VIIDRespEnum;
import com.vids.model.entity.Register;
import com.vids.model.response.ResponseStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

import static com.vids.utils.VIIDUtils.*;

@Slf4j
@Component
public class RegisterMaker {


    @Resource
    SyncViidServerStatus syncViidServerStatus;

    @Resource
    SyncApeServerStatus syncApeServerStatus;

    public void syncStatus() {
        syncViidServerStatus.syncUpdateStatus();
        syncApeServerStatus.syncUpdateStatus();
    }

    public ResponseStatus register(Register vo) {
        ResponseStatus status = new ResponseStatus(Urlconstant.URL_REGISTER, VIIDRespEnum.OK.getCode(), new Date());
//        log.info("进入到注册1 -> {}", vo.getDeviceID());
        if (ObjectUtil.isNotNull(vo)) {
            String deviceID = vo.getDeviceID();
            if (isVIID(deviceID)) {
                log.info("进入到注册2 -> {}", vo.getDeviceID());
                return syncViidServerStatus.register(vo);
            }
            if (isApe(deviceID)) {
                log.info("进入到注册3 -> {}", vo.getDeviceID());
                return syncApeServerStatus.register(vo);
            }

            status.setStatusCode(VIIDRespEnum.INVALID_OPERATION.getCode());
            status.setStatusString("注册对象ID传入错误！");

        } else {
            status.setStatusCode(VIIDRespEnum.INVALID_OPERATION.getCode());
            status.setStatusString("注册对象ID为空！");
            return status;
        }
        return status;
    }


    public ResponseStatus keepLive(Register vo) {

        ResponseStatus status = new ResponseStatus(Urlconstant.URL_KEEPALIVE, VIIDRespEnum.OK.getCode(), new Date());
        if (ObjectUtil.isNotNull(vo)) {
            String deviceID = vo.getDeviceID();
            if (isVIID(deviceID)) {
                return syncViidServerStatus.keepLive(vo);
            }
            if (isApe(deviceID)) {
                return syncApeServerStatus.keepLive(vo);
            }
                status.setStatusCode(VIIDRespEnum.INVALID_OPERATION.getCode());
                status.setStatusString("注册对象ID传入错误！");

        } else {
            status.setStatusCode(VIIDRespEnum.INVALID_OPERATION.getCode());
            status.setStatusString("注册对象ID为空！");
            return status;
        }
        return status;
    }


    public ResponseStatus unRegister(Register vo) {
        ResponseStatus status = new ResponseStatus(Urlconstant.URL_REGISTER, VIIDRespEnum.OK.getCode(), new Date());
        if (ObjectUtil.isNotNull(vo)) {
            String deviceID = vo.getDeviceID();
            //使用卫语句
            if (isVIID(deviceID)) {
                return syncViidServerStatus.unRegister(vo);
            }
            if (isApe(deviceID)) {
                return syncApeServerStatus.unRegister(vo);
            }

                status.setStatusCode(VIIDRespEnum.INVALID_OPERATION.getCode());
                status.setStatusString("注册对象ID传入错误！");

        } else {
            status.setStatusCode(VIIDRespEnum.INVALID_OPERATION.getCode());
            status.setStatusString("注册对象ID为空！");
            return status;
        }
        return status;
    }
}
